package xc.com.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xc.com.common.PageVo;
import xc.com.common.Result;
import xc.com.common.StatusCode;
import xc.com.course.domain.CourseBase;
import xc.com.course.domain.CoursePic;
import xc.com.course.mapper.CourseBaseMapper;
import xc.com.course.mapper.CourseMarketMapper;
import xc.com.course.mapper.CoursePicMapper;
import xc.com.course.service.CourseBaseService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuolizhong
 * @since 2021-06-03
 */
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {
    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CoursePicMapper coursePicMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Override
    public PageVo<CourseBase> findall(Integer pageNum,CourseBase courseBase) {
        PageVo<CourseBase> pageVo=new PageVo<>();
        QueryWrapper<CourseBase> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseBase.getName())){
            queryWrapper.like("name",courseBase.getName());
        }
        String pa = String.valueOf(pageNum);
        Page<CourseBase> page=null;
        if (StringUtils.isNotEmpty(pa)){
           page= new Page<>(pageNum,7);
        }else {
            page= new Page<>(1,7);
        }
        Page<CourseBase> page1 = courseBaseMapper.selectPage(page, queryWrapper);
        for (CourseBase basis : page1.getRecords()) {
            String id = basis.getId();

            CoursePic coursePic = coursePicMapper.selectById(id);
            basis.setCoursePics(coursePic);
        }
        pageVo.setRows(page1.getRecords());
        pageVo.setTotal(page1.getTotal());
        return pageVo;
    }

    @Override
    public Result<CourseBase> addcoursebase(CourseBase courseBase) {
        courseBase.setStatus("202002");
        int i = courseBaseMapper.insert(courseBase);
        return new Result<CourseBase>(true, StatusCode.OK,"添加成功",courseBase);
    }

    @Override
    public CourseBase findById(String id) {
        CourseBase courseBase = courseBaseMapper.selectById(id);
        return courseBase;
    }

    @Override
    public Result updateByCourseId(CourseBase courseBase) {
        int i = courseBaseMapper.updateById(courseBase);
        if(i>0){
            return new Result(true,StatusCode.OK,"修改成功！");
        }
        return new Result(false,StatusCode.OK,"修改失败，请重试！");
    }
}
